﻿<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <title>骡窝窝系统管理平台</title>
    <#include "../common/header.ftl"/>
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" rel="stylesheet">
    <script src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
</head>
<body>

<div class="container " style="margin-top: 20px">
<#----欢迎---->
    <#include "../common/top.ftl" />
    <div class="row">
        <div class="col-sm-3">
            <#assign menu="region"/>
            <#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">旅游地区管理</h1>
                </div>
            </div>

            <div class="col-sm-4">
                <div class="row">
                    <div class="form-group" style="margin-left: 20px">
                        <a href="javascript:void(-1);" class="btn btn-success inputBtn" id="addRegionBtn">添加地区</a>
                    </div>
                </div>
                <br/>
                <div id="treeview1"></div>
            </div>
            <div class="col-sm-8">
                <table class="table table-striped table-hover">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>地区名称</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
            <div class="modal fade" id="regionModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                            <h4 class="modal-title" id="regionModal">地区编辑</h4>
                        </div>
                        <div class="modal-body">
                            <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post"
                                  id="editForm">
                                <input type="hidden" name="id">
                                <input type="hidden" name="parent.id">
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">地区/景点名称：</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" name="name" placeholder="请输入地区/景点名称">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-4 control-label">上级地区：</label>
                                    <div class="col-sm-6">
                                        <input type="text" class="form-control" name="parentName" placeholder="请输入上级地区" readonly>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                            <button type="button" class="btn btn-primary regionSubmitBtn">保存</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(function () {

        $.get("/region/listByParentId.do", {type: "tree"}, function (data) {
            //data一定要按照treeview的格式,不然包js错误
            $('#treeview1').treeview({
                data: [{text: "全部地区", nodes: data}],
                showTags: true,
                lazyLoad: function (node) {
                    //发送ajax请求带上当前点击的节点的id
                    $.get("/region/listByParentId.do", {parentId: node.id, type: "tree"}, function (data) {
                        //调用插件的方法添加节点到父节点上
                        $('#treeview1').treeview('addNode', [data, node]);//data是子节点,node是父节点
                    })
                },
                onNodeSelected: function (event, node) {
                    $.get("/region/listByParentId.do", {parentId: node.id}, function (data) {

                        var temp = "";
                        $.each(data, function (index, ele) {
                            var btnText="设为推荐";
                            if(ele.state==1){
                                btnText="取消推荐"
                            }
                            temp += '<tr><td>' + (index + 1) + '</td><td>' + ele.name + '</td>' +
                                    '<td><a role="button" class="btn btn-primary btn-xs updateBtn" data-json='+ele.json+'><span class="glyphicon glyphicon-pencil"></span>编辑</a></td>' +
                                    '<td><a role="button" class="btn btn-info btn-xs changeStatusBtn" data-json='+ele.json+'><span class="glyphicon glyphicon-pencil"></span>'+btnText+'</a></td></tr>';
                            return temp;
                        });
                        $("tbody").html(temp);
                        //编辑
                        $(".updateBtn").click(function () {
                            //清空模态框
                            $("#editForm input").val("");
                            var json = $(this).data("json");
                            //回显数据
                            if(json){
                                $("#editForm input[name='id']").val(json.id);
                                $("#editForm input[name='parent.id']").val(node.id);
                                $("#editForm input[name='name']").val(json.name);
                                $("#editForm input[name='parentName']").val(node.text);
                            }
                            $("#regionModal").modal("show");
                        });

                        //修改状态
                        $(".changeStatusBtn").click(function () {
                            var json = $(this).data("json");
                            var state = 1;
                            if(json.state == 1){
                                state = 0;
                            }
                            $.get("/region/changeStatus.do",{id:json.id,state:state},function (data) {
                                $.messager.alert("温馨提示", "操作成功,2s后自动刷新");
                                setTimeout(function () {
                                    location.reload();//刷新界面
                                }, 2000)
                            })
                        });
                        
                    });
                }
            });

        });

        $.messager.model = {
            ok: {text: "确定"},
            cancel: {text: "取消"}
        };


        $(".inputBtn").click(function () {
            //清空模态框
            $("#editForm input").val("");
            //获取被选中的节点
            var node = $('#treeview1').treeview('getSelected');
            console.log(node);
            if (node.length > 0) {
                $("#editForm input[name='parent.id']").val(node[0].id);
                $("#editForm input[name='parentName']").val(node[0].text);
            }else{
                $.messager.alert("温馨提示", "目前没有最大地区分配了,2s后自动返回");
                setTimeout(function () {
                    location.reload();//刷新界面
                }, 2000)
            }
            $("#regionModal").modal("show");
        });

        $("#editForm").ajaxForm(function () {
            //回调函数
            $.messager.alert("温馨提示", "操作成功,2s后自动刷新");
            setTimeout(function () {
                location.reload();//刷新界面
            }, 2000)
        });
        $(".regionSubmitBtn").click(function () {
            $("#editForm").submit();
        })
    });
</script>
</body>
</html>
